package com.yanqu.road.entity.activity.stronghold;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@FieldTypeAnnotation(tableName = "t_c_activity_stronghold_eliminate_union")
public class CrossStrongholdEliminateUnion extends DataObject {
    @FieldTypeAnnotation(select = true, update = true, desc = "活动id")
    private int activtyId;
    @FieldTypeAnnotation(select = true, update = true, desc = "淘汰赛第几轮")
    private int round;
    @FieldTypeAnnotation(update = true, desc = "唯一id")
    private int id;
    @FieldTypeAnnotation(update = true, desc = "区服id")
    private long serverId;
    @FieldTypeAnnotation(update = true, desc = "联盟id")
    private String unionUid = "";
    private List<CrossStrongholdEliminateDefendPatrons> defendPatronsList = new ArrayList<>();
    @FieldTypeAnnotation(desc = "联盟信息")
    private UnionBaseInfo unionBaseInfo;
    @FieldTypeAnnotation(desc = "联盟据点等级")
    private int star;
    @FieldTypeAnnotation(desc = "血量加成")
    private int hpAdd;
    private BigInteger totalDamage = BigInteger.ZERO;

    private long lastFightTime;

    private int rank;
    /**
     * 每个联盟有自己的据点战排序
     */
    private List<Integer> sortList = new ArrayList<>();

    public int getActivtyId() {
        return activtyId;
    }

    public int getRound() {
        return round;
    }

    public int getId() {
        return id;
    }

    public long getServerId() {
        return serverId;
    }

    public String getUnionUid() {
        return unionUid;
    }

    public List<CrossStrongholdEliminateDefendPatrons> getDefendPatronsList() {
        return defendPatronsList;
    }

    public UnionBaseInfo getUnionBaseInfo() {
        return unionBaseInfo;
    }

    public int getHpAdd() {
        return hpAdd;
    }

    public void setActivtyId(int activtyId) {
        if (this.activtyId != activtyId) {
            this.activtyId = activtyId;
            setOp(DataStatus.Update);
        }

    }

    public void setRound(int round) {
        if (this.round != round) {
            this.round = round;
            setOp(DataStatus.Update);
        }

    }

    public void setId(int id) {
        if (this.id != id) {
            this.id = id;
            setOp(DataStatus.Update);
        }

    }

    public void setServerId(long serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            setOp(DataStatus.Update);
        }

    }

    public void setUnionUid(String unionUid) {
        if (null == unionUid) {
            return;
        }
        if (!this.unionUid.equals(unionUid)) {
            this.unionUid = unionUid;
            setOp(DataStatus.Update);
        }
    }

    public void setHpAdd(int hpAdd) {
        if (this.hpAdd != hpAdd) {
            this.hpAdd = hpAdd;
            setOp(DataStatus.Update);
        }
    }

    public void setDefendPatronsList(List<CrossStrongholdEliminateDefendPatrons> defendPatronsList) {
        this.defendPatronsList = defendPatronsList;
    }

    public void setUnionBaseInfo(UnionBaseInfo unionBaseInfo) {
        this.unionBaseInfo = unionBaseInfo;
    }

    public int getStar() {
        return star;
    }

    public void setStar(int star) {
        this.star = star;
        setUpdateOption();
    }

    public BigInteger getCurBlood() {
        BigInteger curBlood = BigInteger.ZERO;
        if (defendPatronsList != null) {
            for (CrossStrongholdEliminateDefendPatrons patrons : defendPatronsList) {
                curBlood = curBlood.add(patrons.getCurBlood());
            }
        }
        return curBlood;
    }

    public BigInteger getTotalBlood() {
        BigInteger totalBlood = BigInteger.ZERO;
        if (defendPatronsList != null) {
            for (CrossStrongholdEliminateDefendPatrons patrons : defendPatronsList) {
                totalBlood = totalBlood.add(patrons.getTotalBlood());
            }
        }
        return totalBlood;
    }

    public CrossStrongholdEliminateDefendPatrons getDefendPatrons(long userId,int index) {
        for (CrossStrongholdEliminateDefendPatrons patrons : defendPatronsList) {
            if (patrons.getUserId() == userId && patrons.getPatronsIndex() == index) {
                return patrons;
            }
        }
        return null;
    }

    public void addDefendPatronsList(List<StrongholdEliminateDefendPatrons> strongholdEliminateDefendPatrons) {
        defendPatronsList = new ArrayList<>();
        for (StrongholdEliminateDefendPatrons patrons : strongholdEliminateDefendPatrons) {
            CrossStrongholdEliminateDefendPatrons defendPatrons = new CrossStrongholdEliminateDefendPatrons();
            defendPatrons.setActivityId(patrons.getActivityId());
            defendPatrons.setCurBlood(patrons.getTotalBlood());
            defendPatrons.setTotalBlood(patrons.getTotalBlood());
            defendPatrons.setEarnSpeed(patrons.getEarnSpeed());
            defendPatrons.setPatrons(patrons.getPatrons());
            defendPatrons.setUserBaseInfo(patrons.getUserBaseInfo());
            defendPatrons.setUserId(patrons.getUserId());
            defendPatrons.setUnionUid(patrons.getUnionUid());
            defendPatrons.setPatronsIndex(patrons.getPatronsSite());
            defendPatronsList.add(defendPatrons);
        }

    }

    public CrossStrongholdEliminateDefendPatrons getCurDefendPatrons() {
        for (CrossStrongholdEliminateDefendPatrons patrons : defendPatronsList) {
            if (patrons.getCurBlood().compareTo(BigInteger.ZERO) > 0) {
                return patrons;
            }
        }
        return null;
    }

    public void addSinglePatrons(CrossStrongholdEliminateDefendPatrons patrons) {
        CrossStrongholdEliminateDefendPatrons curPatrons = null;
        if (defendPatronsList == null) {
            defendPatronsList = new ArrayList<>();
        }
        for (CrossStrongholdEliminateDefendPatrons defendPatrons : defendPatronsList) {
            if (defendPatrons.getUserId() == patrons.getUserId() && defendPatrons.getPatronsIndex() == patrons.getPatronsIndex()) {
                curPatrons = defendPatrons;
                break;
            }
        }
        if (curPatrons == null) {
            patrons.setInsertOption();
            defendPatronsList.add(patrons);
        } else {
            curPatrons.setEarnSpeed(patrons.getEarnSpeed());
            curPatrons.setTotalBlood(patrons.getTotalBlood());
            curPatrons.setCurBlood(patrons.getCurBlood());
            curPatrons.setUserBaseInfo(patrons.getUserBaseInfo());
            curPatrons.setPatrons(patrons.getPatrons());
        }
    }

    public BigInteger getTotalDamage() {
        return totalDamage;
    }

    public void setTotalDamage(BigInteger totalDamage) {
        this.totalDamage = totalDamage;
        setUpdateOption();
    }

    public void addTotalDamage(BigInteger addDamage) {
        totalDamage = totalDamage.add(addDamage);
        setUpdateOption();
    }

    public void setLastFightTime(long lastFightTime) {
        this.lastFightTime = lastFightTime;
    }

    public int getStatus() {
        return System.currentTimeMillis() - lastFightTime < 60000 ? 1 : 0;
    }

    public long getLastFightTime() {
        return lastFightTime;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public List<Integer> getSortList() {
        return sortList;
    }

    public void setSortList(List<Integer> sortList) {
        this.sortList = sortList;
        setUpdateOption();
    }
}
